package com.liuyk.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.liuyk.main.activity.R;

/**
 * 位置变换的ImageView
 * <p/>
 * Created by Administrator on 2015/9/13.
 */
public class PolyToPolyView extends ImageView implements View.OnClickListener {
    private final Matrix mMatrix;
    private final Matrix mMatrix1;
    private final Bitmap mBitmap;

    private final Context mContext;

    public PolyToPolyView(Context context) {
        super(context);
        this.mContext = context;

        mMatrix = new Matrix();
        mMatrix1 = new Matrix();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.animation_2);
        setOnClickListener(this);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap, mMatrix, null);
        canvas.drawBitmap(mBitmap, mMatrix1, null);
    }

    @Override
    public void onClick(View v) {

        /**
         * float[] src, int srcIndex,
         float[] dst, int dstIndex,
         int pointCount

         src代表变换前的坐标；dst代表变换后的坐标；从src到dst的变换，
         可以通过srcIndex和dstIndex来制定第一个变换的点，
         一般可能都设置位0。pointCount代表支持的转换坐标的点数，最多支持4个
         */
        float[] src = {//变换前的坐标
                0, 0,
                mBitmap.getWidth(), 0,
                mBitmap.getWidth(), mBitmap.getHeight(),
                0, mBitmap.getHeight()
        };
        float[] dsc = {//变换后的坐标
                0, 0,
                mBitmap.getWidth(), 80,
                mBitmap.getWidth(), mBitmap.getHeight() - 80,
                0, mBitmap.getHeight()
        };

        float[] src1 = {//变换前的坐标
                0, 0,
                mBitmap.getWidth(), 0,
                mBitmap.getWidth(), mBitmap.getHeight(),
                0, mBitmap.getHeight()
        };
        float[] dsc1 = {//变换后的坐标
                0, 0,
                mBitmap.getWidth(), 80,
                mBitmap.getWidth(), mBitmap.getHeight() - 80,
                0, mBitmap.getHeight()
        };
        mMatrix.setPolyToPoly(src, 0, dsc, 0, 4);
        mMatrix1.setPolyToPoly(src1, 0, dsc1, 0, 4);

        invalidate();
        Toast.makeText(mContext, (src.length >> 1) + "", Toast.LENGTH_LONG).show();
    }

}
